#include "networkcardwidget.h"
#include <QFont>
#include <QStyleOption>
#include <QPainter>

NetworkCardWidget::NetworkCardWidget(const QPixmap &icon, const QString &name, const QString &desc, const QString &status, QWidget *parent)
    : QWidget(parent)
    , m_selected(false)
    , m_hovered(false)
    , m_viewMode(TileMode) // 默认设置为平铺模式
    , m_originalIcon(icon)
    , m_originalName(name)
    , m_originalDesc(desc)
    , m_originalStatus(status)
{
    setAutoFillBackground(false);
    setAttribute(Qt::WA_TranslucentBackground);

    iconLabel = new QLabel(this);
    iconLabel->setPixmap(icon.scaled(48, 48, Qt::KeepAspectRatio, Qt::SmoothTransformation));

    nameLabel = new QLabel(name, this);
    nameLabel->setObjectName("nameLabel");
    QFont nameFont;
    nameFont.setPointSize(11);
    nameFont.setBold(true);
    nameLabel->setFont(nameFont);
    nameLabel->setWordWrap(true); // 启用文本换行
    nameLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding); // 允许垂直方向伸展到最大
    nameLabel->setAlignment(Qt::AlignLeft); // 初始平铺模式的对齐

    descLabel = new QLabel(desc, this);
    descLabel->setObjectName("descLabel");
    QFont descFont;
    descFont.setPointSize(9);
    descLabel->setFont(descFont);
    descLabel->setWordWrap(true); // 启用文本换行
    descLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding); // 允许垂直方向伸展到最大
    descLabel->setAlignment(Qt::AlignLeft); // 初始平铺模式的对齐

    statusLabel = new QLabel(status, this);
    statusLabel->setObjectName("statusLabel");
    QFont statusFont;
    statusFont.setPointSize(9);
    statusLabel->setFont(statusFont);
    statusLabel->setWordWrap(true); // 启用文本换行
    statusLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding); // 允许垂直方向伸展到最大
    statusLabel->setAlignment(Qt::AlignLeft); // 初始平铺模式的对齐

    m_textLayout = new QVBoxLayout;
    m_textLayout->setContentsMargins(0, 0, 0, 0);
    m_textLayout->setSpacing(2);
    m_textLayout->addWidget(nameLabel);
    m_textLayout->addWidget(descLabel);
    m_textLayout->addWidget(statusLabel);

    // 平铺模式布局 (初始为空，将在setViewMode中填充)
    m_tileLayout = new QHBoxLayout;
    m_tileLayout->setContentsMargins(16, 8, 16, 8);
    m_tileLayout->setSpacing(12);

    // 大图标模式布局 (初始为空，将在setViewMode中填充)
    m_largeIconLayout = new QVBoxLayout;
    m_largeIconLayout->setContentsMargins(8, 16, 8, 8);
    m_largeIconLayout->setSpacing(8);

    m_currentMainLayout = nullptr;
    setViewMode(TileMode); // 构造时主动刷新布局
}

QSize NetworkCardWidget::sizeHint() const {
    if (m_viewMode == TileMode) {
        int textWidth = 220;
        int totalHeight = 0;
        QFontMetrics nameFm(nameLabel->font());
        QFontMetrics descFm(descLabel->font());
        QFontMetrics statusFm(statusLabel->font());
        totalHeight += nameFm.boundingRect(QRect(0,0,textWidth,9999), Qt::TextWordWrap, nameLabel->text()).height();
        totalHeight += descFm.boundingRect(QRect(0,0,textWidth,9999), Qt::TextWordWrap, descLabel->text()).height();
        totalHeight += statusFm.boundingRect(QRect(0,0,textWidth,9999), Qt::TextWordWrap, statusLabel->text()).height();
        totalHeight += 24;
        return QSize(320, qMax(110, totalHeight));
    } else if (m_viewMode == LargeIconMode) {
        return QSize(160, 240);
    } else {
        return QSize(330, 240);
    }
}

void NetworkCardWidget::setSelected(bool selected) {
    m_selected = selected;
    update();
}

void NetworkCardWidget::setViewMode(ViewMode mode) {
    if (layout()) {
        QLayout *oldLayout = layout();
        delete oldLayout;
    }
    m_viewMode = mode;
    if (m_viewMode == TileMode) {
        // --- 平铺模式 ---
        QHBoxLayout *tileLayout = new QHBoxLayout();
        tileLayout->setContentsMargins(16, 8, 16, 8);
        tileLayout->setSpacing(12);
        QVBoxLayout *textLayout = new QVBoxLayout();
        textLayout->setContentsMargins(0, 0, 0, 0);
        textLayout->setSpacing(2);
        iconLabel->setPixmap(m_currentIcon.isNull() ? m_originalIcon.scaled(48, 48, Qt::KeepAspectRatio, Qt::SmoothTransformation) : m_currentIcon.scaled(48, 48, Qt::KeepAspectRatio, Qt::SmoothTransformation));
        iconLabel->setFixedSize(60, 60);
        iconLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
        nameLabel->setAlignment(Qt::AlignLeft);
        descLabel->setAlignment(Qt::AlignLeft);
        statusLabel->setAlignment(Qt::AlignLeft);
        nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
        descLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
        statusLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
        nameLabel->setMaximumWidth(QWIDGETSIZE_MAX);
        descLabel->setMaximumWidth(QWIDGETSIZE_MAX);
        statusLabel->setMaximumWidth(QWIDGETSIZE_MAX);
        // 计算descLabel所需高度并设置
        int textWidth = 220;
        QFontMetrics descFm(descLabel->font());
        int descHeight = descFm.boundingRect(QRect(0,0,textWidth,9999), Qt::TextWordWrap, descLabel->text()).height();
        descLabel->setMinimumHeight(descHeight);
        textLayout->addWidget(nameLabel);
        textLayout->addWidget(descLabel);
        textLayout->addWidget(statusLabel);
        tileLayout->addWidget(iconLabel, 0, Qt::AlignVCenter);
        tileLayout->addLayout(textLayout, 1);
        setLayout(tileLayout);
        m_currentMainLayout = tileLayout;
        m_tileLayout = tileLayout;
        m_textLayout = textLayout;
    } else if (m_viewMode == LargeIconMode) {
        // --- 大图标模式 ---
        QVBoxLayout *largeIconLayout = new QVBoxLayout();
        largeIconLayout->setContentsMargins(8, 16, 8, 16);
        largeIconLayout->setSpacing(8);
        iconLabel->setPixmap(m_currentIcon.isNull() ? m_originalIcon.scaled(96, 96, Qt::KeepAspectRatio, Qt::SmoothTransformation) : m_currentIcon.scaled(96, 96, Qt::KeepAspectRatio, Qt::SmoothTransformation));
        iconLabel->setFixedSize(96, 96);
        iconLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
        // 字体统一为9pt
        QFont unifiedFont;
        unifiedFont.setPointSize(9);
        nameLabel->setFont(unifiedFont);
        descLabel->setFont(unifiedFont);
        statusLabel->setFont(unifiedFont);
        nameLabel->setAlignment(Qt::AlignHCenter);
        descLabel->setAlignment(Qt::AlignHCenter);
        statusLabel->setAlignment(Qt::AlignHCenter);
        nameLabel->setWordWrap(true);
        descLabel->setWordWrap(true);
        statusLabel->setWordWrap(true);
        nameLabel->setMaximumWidth(180);
        descLabel->setMaximumWidth(180);
        statusLabel->setMaximumWidth(180);
        // 不限制nameLabel高度，保证自适应
        descLabel->setMaximumHeight(32);
        statusLabel->setMaximumHeight(24);
        nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
        descLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
        statusLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
        nameLabel->setTextInteractionFlags(Qt::NoTextInteraction);
        descLabel->setTextInteractionFlags(Qt::NoTextInteraction);
        statusLabel->setTextInteractionFlags(Qt::NoTextInteraction);
        QWidget *fixedSpacer = new QWidget(this);
        fixedSpacer->setFixedHeight(12);
        fixedSpacer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        largeIconLayout->addWidget(iconLabel, 0, Qt::AlignHCenter | Qt::AlignTop);
        largeIconLayout->addWidget(fixedSpacer, 0, Qt::AlignHCenter);
        largeIconLayout->addWidget(nameLabel, 0, Qt::AlignHCenter);
        largeIconLayout->addWidget(descLabel, 0, Qt::AlignHCenter);
        largeIconLayout->addWidget(statusLabel, 0, Qt::AlignHCenter);
        setLayout(largeIconLayout);
        m_currentMainLayout = largeIconLayout;
        m_largeIconLayout = largeIconLayout;
        m_textLayout = nullptr;
    }
    update();
    updateGeometry();
    iconLabel->show();
    nameLabel->show();
    descLabel->show();
    statusLabel->show();
}

void NetworkCardWidget::enterEvent(QEvent *event) {
    Q_UNUSED(event);
    m_hovered = true;
    update();
}

void NetworkCardWidget::leaveEvent(QEvent *event) {
    Q_UNUSED(event);
    m_hovered = false;
    update();
}

void NetworkCardWidget::paintEvent(QPaintEvent *event) {
    Q_UNUSED(event);
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    if (m_selected) {
        p.setBrush(QColor(0, 120, 215, 60));
        p.setPen(QPen(QColor(0, 120, 215), 3)); // 选中时加粗边框
    } else if (m_hovered) {
        p.setBrush(QColor(240, 240, 240, 100));
        p.setPen(QColor(200, 200, 200));
    } else {
        p.setBrush(Qt::NoBrush);
        p.setPen(QColor(220, 220, 220));
    }
    p.setRenderHint(QPainter::Antialiasing);
    // 大圆角
    p.drawRoundedRect(rect().adjusted(2, 2, -2, -2), 18, 18);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void NetworkCardWidget::resizeEvent(QResizeEvent *event) {
    if (m_firstLayout && width() > 0 && height() > 0) {
        setViewMode(m_viewMode); // 每个卡片只刷新一次
        m_firstLayout = false;
    }
    QWidget::resizeEvent(event);
} 

void NetworkCardWidget::updateData(const QPixmap &icon, const QString &name, const QString &desc, const QString &status)
{
    m_currentIcon = icon; // 保存当前icon
    iconLabel->clear(); // 强制清理，确保图标刷新
    iconLabel->setPixmap(icon.scaled(m_viewMode == LargeIconMode ? 64 : 48, m_viewMode == LargeIconMode ? 64 : 48, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    nameLabel->setText(name);
    descLabel->setText(desc);
    statusLabel->setText(status);
    update();
} 